/**
 * Created by baby520 on 2016/8/13.
 */
define(function(require, exports, modules) {
    'use strict';

    require('css!assets/css/chickensLog/log.css');

    var template = require('text!./chickensLog.html'),
        log1=require('text!./log-1.html'),
        log2=require('text!./log-2.html'),
        log3=require('text!./log-3.html'),
        log4=require('text!./log-4.html'),
        util = require('util');
    var View = Backbone.View.extend({
        el : '#page',
        events : {
            'click .shade .error>.yellowBtn':'hide_shade',
            'click .upper':function () {
                
                this.currentPage--;
                if(this.currentPage<1){
                    this.currentPage=1;
                    return;
                }
                loadingShow();
                this.getData(this.urlName,this.currentPage,this.panls);
            },
            'click .lower':function () {
               
                this.currentPage++;
                if(this.currentPage>this.totalSize){
                    this.currentPage=this.totalSize;
                      return;  
                }
                loadingShow();
                this.getData(this.urlName,this.currentPage,this.panls);
            },

            'click .black_2':function () {
                Backbone.history.navigate('home',{trigger:true});
            },
            'click .log-sidebar ul li':function(event){
                this.currentPage=1;
                this.numPerPage=8;
                this.totalSize=1;
                var _this=this;
                var _self=event.currentTarget;
                var tab=$(_self).attr('data-tab');
                if(tab==1){
                    this.urlName='getRateDailyRecord';
                    this.panls=log1;
                }else if(tab==2){
                    this.urlName='cleanRecord';
                    this.panls=log2;
                }else if(tab==3){
                    this.urlName='incubateEggRecord';
                    this.panls=log3;
                }else if(tab==4){
                    this.urlName='chickenToLandRecord';
                    this.panls=log4;
                }
                this.getData(this.urlName,this.currentPage,this.panls,_self);
            }
        },
        getData:function (url,currentPage,log,_self) {
            var _this=this;
            loadingShow();
            jsonpGet(util.NDNserver+"/"+url,{
                userId:this.userId,token: this.token,
                currentPage:currentPage,numPerPage:7},url,$.proxy(function(res){
                loadingRemove();
                if(res.code==1){
                    var tab=$(_self).attr('data-tab');
                    $(_self).removeClass('nth-log'+tab);
                    $(_self).addClass('no-nth-log'+tab);
                    var data=res.data.recordList;
                    _this.totalSize=res.data.pageCount;
                    if(_this.totalSize==0){
                        _this.totalSize=1
                    };
                    _this.$el.find('.number').text( this.currentPage+"/"+_this.totalSize)
                    _.each(data,function(obj,index){
                        if(obj['daily']){
                            obj['daily']=_this.daily(obj['daily']);
                        }else{
                            obj['daily']=formatDate(new Date(obj['createTime']),3);
                        }
                    });
                    _this.log=_.template(log);
                    $('.log-content').html(_this.log({list:data}));
                    if(_self){
                        $('.log-sidebar ul li').each(function(i,item){
                            var index=i;
                            index++;
                            if(!$(item).hasClass('nth-log'+index)){

                                if(tab!=index){
                                    $(item).removeClass('no-nth-log'+index);
                                    $(item).addClass('nth-log'+index);
                                }
                            }
                        })
                    }
                }else{
                    _this.show_shade('error');
                    $('.shade .error p').text(res.msg);
                }
            },this));
        },
        daily:function(daily){
            var t=daily.substring(4,6);
            var f=daily.substring(6);
            return t+"-"+f;
        },
        initialize : function() {
            this.user=JSON.parse(util.getCookie('user'));
            if(!this.user){
				Backbone.history.navigate('loading',{trigger:true});
				return;
			}
            if(this.user.userStatus == 3){
				Backbone.history.navigate('home',{trigger:true});
				return;
			}
            this.token=this.user.token;
            this.userId=this.user.userId;
            this.currentPage=1;
            this.numPerPage=8;
            this.totalSize=1;
            this.urlName='chickenToLandRecord';
            this.panls=log4;
            this.template = _.template(template);
            this.render();
            $('.chickensLog header .header-name .header_box').css({'background-image': this.user.userPic && this.user.userPic.length>0 ? "url('"+this.user.userPic+"')" : "url('assets/images/home/portrait/portrait-man.png')"});
            $('.chickensLog header .header-name .leaf').text(this.user.loginName||"")
            this.getData(this.urlName,this.currentPage,this.panls);
            this.getTotalRecord('getTotalRecord');
        },
        getTotalRecord:function (url) {
            var _this=this;

         jsonpGet(util.NDNserver+"/"+url,{

                userId:this.userId,token:
                    this.token},url,$.proxy(function(res){
                if(res.code==1){
                    var data=res.data;
                    if(!data.totalClean){
                        data.totalClean=0
                    }
                    if(!data.totalSuperIncome){
                        data.totalSuperIncome=0
                    }
                    if(!data.totalEggIncome){
                        data.totalEggIncome=0
                    }
                    var item='<ul><li>'+res.data.totalClean+'<p>打扫收益</p></li> ' +
                        '<li>'+res.data.totalEggIncome+'<p>总生产鸡蛋</p></li> ' +
                        '<li>'+res.data.totalSuperIncome+'<p>孵化机收益</p></li></ul>';
                    this.$el.find('.flip .sum').html(item)
                }else{
                    _this.show_shade('error');
                    $('.shade .error p').text(res.msg);
                }
            },this));
        },
        hide_shade: function () {
            $('section.shade').hide(0);
            $('section.shade >div').hide(0);
        },
        show_shade: function (ele) {

            $('section.shade').show(0);
            $('section.shade >div').hide(0);
            $('section.shade > .'+ele).show(0);
        },
        render : function() {
            this.$el.html(this.template({totalSize:this.totalSize,currentPage:this.currentPage}));

        }
    });

    return View;
});